package com.ruoyi.web.controller.car;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.car.domain.CarDoor;
import com.ruoyi.car.service.ICarDoorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆门Controller
 * 
 * @author zls
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/car/door")
public class CarDoorController extends BaseController
{
    private String prefix = "car/door";

    @Autowired
    private ICarDoorService carDoorService;

    @RequiresPermissions("car:door:view")
    @GetMapping()
    public String door()
    {
        return prefix + "/door";
    }

    /**
     * 查询车辆门列表
     */
    @RequiresPermissions("car:door:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarDoor carDoor)
    {
        startPage();
        List<CarDoor> list = carDoorService.selectCarDoorList(carDoor);
        return getDataTable(list);
    }

    /**
     * 导出车辆门列表
     */
    @RequiresPermissions("car:door:export")
    @Log(title = "车辆门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarDoor carDoor)
    {
        List<CarDoor> list = carDoorService.selectCarDoorList(carDoor);
        ExcelUtil<CarDoor> util = new ExcelUtil<CarDoor>(CarDoor.class);
        return util.exportExcel(list, "door");
    }

    /**
     * 新增车辆门
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆门
     */
    @RequiresPermissions("car:door:add")
    @Log(title = "车辆门", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarDoor carDoor)
    {
        return toAjax(carDoorService.insertCarDoor(carDoor));
    }

    /**
     * 修改车辆门
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CarDoor carDoor = carDoorService.selectCarDoorById(id);
        if (carDoor==null){
            carDoor=new CarDoor();
        }
        mmap.put("carDoor", carDoor);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆门
     */
    @RequiresPermissions("car:door:edit")
    @Log(title = "车辆门", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarDoor carDoor)
    {
        if (carDoor.getId()==null){
            return addSave(carDoor);
        }
        return toAjax(carDoorService.updateCarDoor(carDoor));
    }

    /**
     * 删除车辆门
     */
    @RequiresPermissions("car:door:remove")
    @Log(title = "车辆门", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carDoorService.deleteCarDoorByIds(ids));
    }
}
